<?php
$uzkl = "SELECT * FROM `works` WHERE '1'='1'";
$t = $db->query($uzkl);
$j = 0;
while($data = $db->fetch($t)){
    $wid = $data->id;
    $uzkl = "SELECT * FROM `feedbacks` WHERE `workid`='$wid'";
    $t2 = $db->query($uzkl);
    $i = 0;
    $sum = 0;
    while ($data2 = $db->fetch($t2)){
        $sum = $sum + $data2->speed + $data2->quality;
        $i++;
    }
    if ($sum != 0) {
        $vidurkiai[$j] = array(($sum / ($i * 2)), $data->id, $data->description, $data->uid);
        $j++;
    }
}
sort($vidurkiai);
if ($j > 5) {
    $geriausias = $vidurkiai[$j-1];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
    $geriausias = $vidurkiai[$j-2];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
    $geriausias = $vidurkiai[$j-3];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
    $geriausias = $vidurkiai[$j-4];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
} else {
    $geriausias = $vidurkiai[$j-1];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
}
?>
<div>
    <table border="1">
        <tr>
            <td style="width:60px;">
                <img src="default-user.jpg" style="height:60px;width:60px;" />
            </td>
            <td style="border:0;padding:0 0 0 10px;">
                <h3 style="margin:0 0 0 0;"><?php echo $user->getName() . " " . $user->getSurname(); ?></h3>
                <p style="margin:0 0 0 0;"></p>
                <p style="margin:0 0 0 0;"></p>
            </td>
        </tr>
    </table>
</div>
<hr/>
<?php
$query = $db->query("SELECT COUNT(*) AS `exists` FROM `feedbacks`") or die(mysql_error());
$row = $db->fetch($query);
$exists = $row->exists;

if ($exists) {
    ?>
    <table style="border:0;">
        <tr>
            <td>
                <h3>Your overall stats</h3>
                <?php
                $sql = "SELECT  DISTINCT `feedbacks`.`id`, `feedbacks`.`workid`,`comment`,`feedbacks`.`uid`, AVG(`speed`) AS `speed`, AVG(`quality`) AS `quality` FROM `feedbacks` INNER JOIN `works` ON `works`.`id`=`feedbacks`.`workid` WHERE `works`.`uid` = '" . $_SESSION['uid'] . "'";
                $query = $db->query($sql);
                $res = $db->fetch($query);

                $both = round(($res->quality + $res->speed) / 2,2);
                $q = round($res->quality,2);
                $s = round($res->speed,2);
                ?>       

                <table cellpadding="0" cellspacing="0" style="width:250px;">
                    <tr style="height:30px">
                        <td style="padding:5px 3px 3px 6px;margin:0">
                            Average 
                        </td>
                        <td style="padding:5px 3px 3px 6px;;margin:0">
                            <?php
                            for ($c = 0; $c < $both; $c++)
                                echo "<img style='width:16px;' src='star_48.png'/>";
                            $c = 0;
                            echo " ($both)";
                            ?>
                        </td>
                    </tr>
                    <tr style="height:30px">
                        <td style="padding:5px 3px 3px 6px;margin:0;width: 100px">Speed/On Time</td>
                        <td style="padding:5px 3px 3px 6px;;margin:0">
                            <?php
                            for ($c = 0; $c < $s; $c++)
                                echo "<img style='width:16px;' src='star_48.png'/>";
                            echo " ($s)";
                            $c = 0;
                            $s = 0;
                            ?>
                        </td>
                    </tr>
                    <tr style="height:30px">
                        <td style="padding:5px 3px 3px 6px;margin:0">
                            Quality 
                        </td>
                        <td style="padding:5px 3px 3px 6px;;margin:0">
                            <?php
                            for ($c = 0; $c < $q; $c++)
                                echo "<img style='width:16px;' src='star_48.png'/>";
                            echo " ($q)";
                            $c = 0;
                            $q = 0;
                            ?>
                        </td>
                    </tr>
                </table>
            </td><td>
                <h3>Company overall stats</h3>
                <?php
                $csql = "SELECT DISTINCT `feedbacks`.`id`, `feedbacks`.`workid`,`comment`,`feedbacks`.`uid`, AVG(`speed`) AS `speed`, AVG(`quality`) AS `quality` FROM `feedbacks` INNER JOIN `works` ON `works`.`id`=`feedbacks`.`workid`";
                $cquery = $db->query($csql);
                $cres = $db->fetch($cquery);

                $iboth = round(($cres->quality + $cres->speed) / 2,2);
                $iq = round($cres->quality,2);
                $is = round($cres->speed,2);
                ?>       

                <table cellpadding="0" cellspacing="0" style="width:250px;">
                    <tr style="height:30px">
                        <td style="padding:5px 3px 3px 6px;margin:0">
                            Average 
                        </td>
                        <td style="padding:5px 3px 3px 6px;;margin:0">
                            <?php
                            for ($c = 0; $c < $iboth; $c++)
                                echo "<img style='width:16px;' src='star_48.png'/>";
                            $c = 0;
                            echo " ($iboth)";
                            ?>
                        </td>
                    </tr>
                    <tr style="height:30px">
                        <td style="padding:5px 3px 3px 6px;margin:0;width: 100px">Speed/On Time</td>
                        <td style="padding:5px 3px 3px 6px;;margin:0">
                            <?php
                            for ($c = 0; $c < $is; $c++)
                                echo "<img style='width:16px;' src='star_48.png'/>";
                            echo " ($is)";
                            $c = 0;
                            ?>
                        </td>
                    </tr>
                    <tr style="height:30px">
                        <td style="padding:5px 3px 3px 6px;margin:0">
                            Quality 
                        </td>
                        <td style="padding:5px 3px 3px 6px;;margin:0">
                            <?php
                            for ($c = 0; $c < $iq; $c++)
                                echo "<img style='width:16px;' src='star_48.png'/>";
                            echo " ($iq)";
                            $c = 0;
                            ?>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <h3>Personal projects ratings chart</h3>
    <?php
    $psql = "SELECT DISTINCT (`feedbacks`.`id`), `feedbacks`.`workid`,`feedbacks`.`uid`, `speed`, `quality` FROM `feedbacks` INNER JOIN `works` ON `works`.`id`=`feedbacks`.`workid` WHERE `works`.`uid` = '" . $_SESSION['uid'] . "'";
    $pquery = $db->query($psql) or die(mysql_error());
    $pboth = array();
    $pq = array();
    $ps = array();
    $counter = array();
    $i=1;
    while ($pres = $db->fetch($pquery)) {
        $tmp =($pres->quality + $pres->speed) / 2;
        array_push($pboth, $tmp);
        array_push($pq,$pres->quality);
        array_push($ps,$pres->speed);
        array_push($counter,"");
        $i++;
    }
     include("pChart/pData.class");
    include("pChart/pChart.class");
    if (!empty($pboth) && !empty($pq) && !empty($ps)) {

    // Dataset definition   
   $DataSet = new pData;
    $DataSet->AddPoint($pboth, "Serie1");
    $DataSet->AddPoint($pq, "Serie2");
    $DataSet->AddPoint($ps, "Serie3");
    $DataSet->AddAllSeries();
     $DataSet->SetAbsciseLabelSerie("Serie4");  
    $DataSet->SetSerieName("Speed", "Serie1");
    $DataSet->SetSerieName("Quality", "Serie2");
    $DataSet->SetSerieName("Average", "Serie3");
 $DataSet->SetYAxisName("Ratings");  
 $DataSet->SetXAxisName("Projects");
    // Initialise the graph  
    $Test = new pChart(600, 230);
    $Test->setFontProperties("Fonts/tahoma.ttf", 8);
    $Test->setGraphArea(50, 30, 480, 190);
    $Test->drawFilledRoundedRectangle(7, 7, 593, 223, 5, 240, 240, 240);
    $Test->drawRoundedRectangle(5, 5, 595, 225, 5, 230, 230, 230);
    $Test->drawGraphArea(255, 255, 255, TRUE);
    $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_NORMAL, 150, 150, 150, TRUE, 0, 2, TRUE);
    $Test->drawGrid(4, TRUE, 230, 230, 230, 50);

    // Draw the 0 line  
    $Test->setFontProperties("Fonts/tahoma.ttf", 6);
    $Test->drawTreshold(0, 143, 55, 72, TRUE, TRUE);

    // Draw the bar graph  
   $Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());  
 $Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);  

    // Finish the graph  
    $Test->setFontProperties("Fonts/tahoma.ttf", 8);
    $Test->drawLegend(496, 150, $DataSet->GetDataDescription(), 255, 255, 255);
    $Test->setFontProperties("Fonts/tahoma.ttf", 10);
    $Test->Render("abc.png");
    echo "<img src='abc.png'/>";
    }
    else {
        echo "<p>No data.</p>";
    }
    ?>  
    <h3>Company overall stats chart</h3>
    <?php
   
  if (!empty($iboth) && !empty($iq) && !empty($is)) {
    // Dataset definition   
    $DataSet = new pData;
    $DataSet->AddPoint(array($is), "Serie1");
    $DataSet->AddPoint(array($iq), "Serie2");
    $DataSet->AddPoint(array($iboth), "Serie3");
    
    $DataSet->AddAllSeries();
    $DataSet->SetAbsciseLabelSerie("Serie4");
    $DataSet->SetSerieName("Speed", "Serie1");
    $DataSet->SetSerieName("Quality", "Serie2");
    $DataSet->SetSerieName("Average", "Serie3");

    // Initialise the graph  
     $Test = new pChart(600, 230);
    $Test->setFontProperties("Fonts/tahoma.ttf", 8);
    $Test->setGraphArea(50, 30, 480, 200);
    $Test->drawFilledRoundedRectangle(7, 7, 593, 223, 5, 240, 240, 240);
    $Test->drawRoundedRectangle(5, 5, 595, 225, 5, 230, 230, 230);
    $Test->drawGraphArea(255, 255, 255, TRUE);
    $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_NORMAL, 150, 150, 150, TRUE, 0, 2, TRUE);
    $Test->drawGrid(4, TRUE, 230, 230, 230, 50);

    // Draw the 0 line  
    $Test->setFontProperties("Fonts/tahoma.ttf", 6);
    $Test->drawTreshold(0, 143, 55, 72, TRUE, TRUE);

    // Draw the bar graph  
    $Test->drawBarGraph($DataSet->GetData(), $DataSet->GetDataDescription(), TRUE);

    // Finish the graph  
    $Test->setFontProperties("Fonts/tahoma.ttf", 8);
    $Test->drawLegend(496, 150, $DataSet->GetDataDescription(), 255, 255, 255);
    $Test->setFontProperties("Fonts/tahoma.ttf", 10);
    $Test->Render("example12.png");
    echo "<img src='example12.png'/>";
  }
    ?>  

    <?php
} else {
    echo "No data";
}
?>